package utils;

import beans.Model;
import beans.Task;
import dao.ModelDao;
import lombok.extern.log4j.Log4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@SuppressWarnings("Duplicates")
public class ExternalInterfaceParseUtil {
    private static String CREATE_PERSON = "1" ;
    private static String NEST_JSON = "";
    private static boolean IS_NESTING = false;

    /**
     * 由模型的json和包含有输入、输出、参数等必要信息的json生成标准的task的启动json
     * 样例paramsJson位于AnnotationFiles/接口调用者保存任务接口参数说明.json
     * @param modelId
     * @param paramsJson
     * @return
     */
    public Task createTaskByModelIdAndParamsJson(String userId, int modelId, JSONObject paramsJson){
        Model model = (Model) new ModelDao().getModelByModelId(modelId+"").get(0);
        JSONObject modelJson = JSONObject.fromObject(model.getJson());
        JSONObject taskJson = ParseUtil.parseTaskJson(modelJson,paramsJson);
        String taskName = model.getName();
        String introduction = model.getIntroduction();
        Task task = new Task(modelId,taskName,userId, taskJson.toString(),NEST_JSON,introduction);
        task.setIs_nesting(IS_NESTING);
        return task;
    }

    /**
     * 由模型的json和包含有输入、输出、参数等必要信息的json生成标准的task的启动json
     * 样例paramsJson位于AnnotationFiles/CreateModelAndStartTask.json
     * @param model
     * @param paramsJson
     * @return
     */
    public Task createTaskByModelAndParamsJson(String userId, Model model, JSONObject paramsJson){
        JSONObject taskJson = JSONObject.fromObject(model.getJson());
        JSONArray inputArray = paramsJson.getJSONObject("params").getJSONArray("inputs");
        for(int i=0;i<inputArray.size();i++){
            JSONArray tempArray = inputArray.getJSONArray(i);
            for(int j=0;j<tempArray.size();j++){
                JSONObject temp = tempArray.getJSONObject(j);
                if(!temp.containsKey("blockName")){
                    temp.put("blockName", "任务"+(i+1)+"，算法"+(j+1));
                }
            }
        }
        JSONArray outputArray = paramsJson.getJSONObject("params").getJSONArray("outputs");
        for(int i=0;i<outputArray.size();i++){
            JSONArray tempArray = outputArray.getJSONArray(i);
            for(int j=0;j<tempArray.size();j++){
                JSONObject temp = tempArray.getJSONObject(j);
                if(!temp.containsKey("blockName")){
                    temp.put("blockName", "任务"+(i+1)+"，算法"+(j+1));
                }
            }
        }
        taskJson.put("inputs",inputArray);
        taskJson.put("outputs",outputArray);
        taskJson.put("data",paramsJson.getJSONObject("params").getJSONArray("data"));
        taskJson.put("other",paramsJson.getJSONObject("params").getJSONArray("other"));
        String taskName = model.getName();
        String introduction = model.getIntroduction();
        Task task = new Task(model.getModelId(),taskName,userId, taskJson.toString(),NEST_JSON,introduction);
        task.setIs_nesting(IS_NESTING);
        return task;
    }

    /**
     * 由模型的json生成标准的task的启动json
     * 样例paramsJson位于AnnotationFiles/接口调用者保存任务接口参数说明.json
     * @param modelId
     * @return
     */
    public Task createTaskByModelId(String userId, int modelId){
        Model model = (Model) new ModelDao().getModelByModelId(modelId+"").get(0);
        JSONObject modelJson = JSONObject.fromObject(model.getJson());
        String taskName = model.getName();
        String introduction = model.getIntroduction();
        Task task = new Task(modelId,taskName,userId, modelJson.toString(),NEST_JSON,introduction);
        task.setIs_nesting(IS_NESTING);

        Log4jInit.logger.info("====Exter====task id====:"+task.getTask_id());
        return task;
    }
}
